package com.yide.myworld.service.impl;

import com.alibaba.cloud.nacos.NacosDiscoveryProperties;
import com.alibaba.cloud.nacos.NacosServiceManager;
import com.alibaba.nacos.api.exception.NacosException;
import com.alibaba.nacos.api.naming.NamingService;
import com.yide.myworld.service.NacosService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

@Slf4j
@Component
@RefreshScope
public class NacosServiceImpl implements NacosService {

    private final NacosServiceManager nacosServiceManager;
    private final NacosDiscoveryProperties nacosDiscoveryProperties;
    private final HttpServletRequest request;
    private static Map<String, Object> serviceMap = new ConcurrentHashMap<>();

    public NacosServiceImpl(NacosServiceManager nacosServiceManager, NacosDiscoveryProperties nacosDiscoveryProperties, HttpServletRequest request) {
        this.nacosServiceManager = nacosServiceManager;
        this.nacosDiscoveryProperties = nacosDiscoveryProperties;
        this.request = request;
    }


    @Override
    public String registerInstance() {
        nacosDiscoveryProperties.setPort(request.getLocalPort());
        NamingService namingService = nacosServiceManager.getNamingService(nacosDiscoveryProperties.getNacosProperties());
        try {
            Object object = serviceMap.get(nacosDiscoveryProperties.getService());
            if (ObjectUtils.isEmpty(object)) {
                namingService.registerInstance(nacosDiscoveryProperties.getService(), nacosDiscoveryProperties.getIp(), nacosDiscoveryProperties.getPort());
                log.info(nacosDiscoveryProperties.getService() + "服务注册成功,服务端口号为：" + nacosDiscoveryProperties.getPort());
                return "success";
            }
        } catch (NacosException e) {
            log.error("服务注册失败!", e);
            throw new RuntimeException(e);
        }
        log.info(nacosDiscoveryProperties.getService() + "服务注册失败，服务已经被下线！");
        return "false";

    }


    @Override
    public String deregisterInstance() {
        String serviceName = nacosDiscoveryProperties.getService();
        String groupName = nacosDiscoveryProperties.getGroup();
        String clusterName = nacosDiscoveryProperties.getClusterName();
        String ip = nacosDiscoveryProperties.getIp();
        int port = nacosDiscoveryProperties.getPort();

        log.info("ready deregisterInstance serviceName:[{}] groupName:[{}] clusterName:[{}] ip:[{}] port:[{}]", serviceName, groupName, clusterName, ip, port);
        try {
            NamingService naming = nacosServiceManager.getNamingService(nacosDiscoveryProperties.getNacosProperties());
            naming.deregisterInstance(serviceName, groupName, ip, port, clusterName);
            serviceMap.put(serviceName, new Object());
        } catch (NacosException e) {
            log.error("deregister from nacos error", e);
            return "error";
        }

        return "success";
    }

    @Override
    public String flushCache() {
        serviceMap.clear();
        return "success";
    }

}
